package cn.hk.common.utils;

import cn.hk.common.entity.HttpStatus;
import cn.hutool.core.util.ObjectUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ruoyi.common.core.page.PageDomain;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.page.TableSupport;
import com.ruoyi.common.utils.sql.SqlUtil;
import org.springframework.stereotype.Component;

import java.util.List;
@Component
public class PageHelpUtil {
    //分页工具
    public static  TableDataInfo getDataTable(List list) {
        TableDataInfo rspData = new TableDataInfo();
        rspData.setCode(HttpStatus.SUCCESS);
        rspData.setMsg("查询成功");
        rspData.setRows(list);
        rspData.setTotal(new PageInfo<>(list).getTotal());
        return rspData;
    }

    public static  void startPage(Integer pageNum,Integer pageSize,String orderByColumn,String isAsc) {
        PageDomain pageDomain = TableSupport.buildPageRequest();
        pageDomain.setPageNum(pageNum);
        pageDomain.setPageSize(pageSize);
        if (ObjectUtil.isNotEmpty(orderByColumn)){
            pageDomain.setOrderByColumn(orderByColumn);
        }
        if (ObjectUtil.isNotEmpty(isAsc)){
           pageDomain.setIsAsc(isAsc);
        }
        if (ObjectUtil.isNotNull(pageNum)&&ObjectUtil.isNotNull(pageSize)){
            String orderBy = SqlUtil.escapeOrderBySql(pageDomain.getOrderBy());
            Boolean reasonable = pageDomain.getReasonable();
            PageHelper.startPage(pageNum, pageSize, orderBy).setReasonable(reasonable);
        }
    }
}
